﻿using ASPControlsExtended.Demo.Models;
using ASPControlsExtended.Demo.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ASPControlsExtended.Demo.Demos.GridViewDemo {
    public partial class GridViewDemoPage : System.Web.UI.Page {

        private Services.ListService _listService;

        private ListService ListService {
            get {
                if (_listService != null) {
                    return _listService;
                }

                _listService = new ListService();

                return _listService;
            }
        }

        protected void Page_Load(object sender, EventArgs e) {
            if (IsPostBack) {
                return;
            }

            List<ASPControlsExtended.Controls.GridView> gridViewsOnPage = new List<ASPControlsExtended.Controls.GridView>() { gvNoPaging, gvPaging, gvSorting, gvDataKey };

            gridViewsOnPage.ForEach(DataBindGrid);

        }

        private void DataBindGrid(ASPControlsExtended.Controls.GridView bindingGridView) {
            if (bindingGridView == null) {
                return;
            }

            List<ProductInfo> products = ListService.GetList();
            if (products == null) {
                bindingGridView.DataSource = null;
                bindingGridView.DataBind();
            }

            if (bindingGridView.AllowSorting) {
                if (bindingGridView.CurrentSortDirection == SortDirection.Ascending) {
                    products = products.OrderBy(p => p.GetType().GetProperty(bindingGridView.CurrentSortExpression).GetValue(p, null)).ToList();
                } else {
                    products = products.OrderByDescending(p => p.GetType().GetProperty(bindingGridView.CurrentSortExpression).GetValue(p, null)).ToList();
                }
            }

            bindingGridView.DataSource = products;
            bindingGridView.DataBind();
        }

        private bool _disposed;
        public override void Dispose() {
            if (_disposed) {
                return;
            }

            if (_listService != null) {
                _listService.Dispose();
                _listService = null;
            }

            base.Dispose();

            _disposed = true;
        }

        protected void gvPaging_PageIndexChanging(object sender, GridViewPageEventArgs e) {
            ASPControlsExtended.Controls.GridView pagingGridView = sender as ASPControlsExtended.Controls.GridView;
            if (pagingGridView == null) {
                return;
            }

            DataBindGrid(pagingGridView);
        }

        protected void gvSorting_Sorting(object sender, GridViewSortEventArgs e) {
            ASPControlsExtended.Controls.GridView sortingGridView = sender as ASPControlsExtended.Controls.GridView;
            if (sortingGridView == null) {
                return;
            }

            DataBindGrid(sortingGridView);
        }

        protected void gvPaging_SelectedIndexChanging(object sender, GridViewSelectEventArgs e) {
            var selectedProduct = gvPaging.GetDatakeyValues<ProductInfo>(e.NewSelectedIndex);
            if (selectedProduct == null) {
                return;
            }

            lblCategory.Text = selectedProduct.Category;
            lblProduct.Text = selectedProduct.Product;
            lblQuantity.Text = selectedProduct.Quantity;

            phSelectedProduct.Visible = true;
        }
    }
}